﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tangosol.Net;
using Coherence.Linq;
using System.Linq.Expressions;
using Coherence.Contrib;
using Tangosol.Util;

namespace CoherenceSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAddPerson_Click(object sender, EventArgs e)
        {
            System.Random rnd = new Random(0);
            try
            {
                INamedCache PersonCache = CacheFactory.GetCache("dist-Person");
                INamedCache PhoneCache = CacheFactory.GetCache("dist-Phone");
                Dictionary<int, Person> perDict = new Dictionary<int, Person>();
                Dictionary<string, PhoneNumber> phonDict = new Dictionary<string, PhoneNumber>();
                for (int i = 0; i < 1000; i++)
                {
                    perDict.Add(i, new Person()
                    {
                        ID = i,
                        FirstName = string.Format("First Name {0}", i),
                        LastName = string.Format("LastName {0}", i),
                        Address = string.Format("Address {0}", Guid.NewGuid()),
                        Title = i % 2 == 1 ? "Mr" : "Mrs"
                    });
                    for (int j = 0; j < rnd.Next(3); j++)
                    {
                        phonDict.Add(string.Format("{0}_{1}", i, j), new PhoneNumber()
                        {
                            ID = j,
                            PersonID = i,
                            Number = "PHone " + j.ToString(),
                            Name = "Name " + j.ToString()
                        });
                    }
                }
                PersonCache.InsertAll(perDict);
                PhoneCache.InsertAll(phonDict);
                MessageBox.Show("Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnGetAll_Click(object sender, EventArgs e)
        {
            try
            {
                INamedCache cache = CacheFactory.GetCache("dist-Person");
                Object[] people = (Object[])cache.GetValues(new Tangosol.Util.Filter.AlwaysFilter());

                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = people.Cast<Person>().ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                CoherenceQuery<Person> coherenceData =
                 new CoherenceQuery<Person>(
                     new CoherenceQueryProvider(CacheFactory.GetCache("dist-Person")));

                CoherenceQuery<PhoneNumber> coherenceData2 =
                 new CoherenceQuery<PhoneNumber>(
                     new CoherenceQueryProvider(CacheFactory.GetCache("dist-Phone")));

                string likeClause = "%8";

                var people = from person in coherenceData
                             where
                                 (person.ID.In(new int[] { 1, 6, 9 })
                                     ||
                                 (person.FirstName.Like("Test")
                                  || person.LastName.Like(likeClause))
                                  && 
                                 person.FirstName == "First Name 154")
                             //select person;
                             //join phone in coherenceData2 on person.ID equals phone.PersonID
                             //select person; 
                             select new { person.Title, person.ID, person.LastName };
                             
                //IFilter filter = ((ICoherenceQueryable)people).Filter;

                //(person.ID ==1 || person.ID < 2) && person.LastName == "Last"  && !(person.ID==3) select person;
                //Person[] p = people.ToArray();
                dataGridView1.DataSource = people.ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void cmdDynamicQuery_Click(object sender, EventArgs e)
        {
            try
            {
                // Simple Coherence Linq Provider
                //CoherenceQuery<Person> coherenceData =
                // new CoherenceQuery<Person>(
                //     new CoherenceQueryProvider(CacheFactory.GetCache("dist-Person")));

                // MetaData Coherence Linq provider
                CoherenceQuery<Person> coherenceData =
                 new CoherenceQuery<Person>(
                     new CoherenceQueryProvider(CacheFactory.GetCache("dist-Person")));
                
                string likeClause = "%8";

                var people = coherenceData.Where(person=>person.ID.In(new int[]{1,6,9}));
                people = people.Where(person => person.ID.In(new int[] { 1, 6 }));
                //people = people.WhereOr(person => person.ID == 2);
                var people2 = people.Select(person=> new {person.ID, person.LastName});

                dataGridView1.DataSource = people2.ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void cmdGetAllPhone_Click(object sender, EventArgs e)
        {
            try
            {
                INamedCache cache = CacheFactory.GetCache("dist-Phone");
                Object[] people = (Object[])cache.GetValues(new Tangosol.Util.Filter.AlwaysFilter());

                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = people.Cast<PhoneNumber>().ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}